package gormdemo

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

type Product struct {
	ID    uint   `gorm:"primarykey"`
	Code  string `gorm:"column: code"`
	Price uint   `gorm:"column: user_Id"`
}

func main() {
	db, err := gorm.Open(mysql.Open("root:9746@tcp(localhost:3306)/test?charset=utf8"), &gorm.Config{})
	if err != nil {
		fmt.Println("failed to connect  database")
	}

	//创建一条数据库
	p := &Product{Code: "D42"}
	ros := db.Create(p)
	//获取error
	fmt.Println(ros.Error)
	//获取插入数据的主键
	fmt.Println(p.ID)
	//创建多条数据
	products := []*Product{{Code: "D41"}, {Code: "D42"}, {Code: "D43"}}
	res := db.Create(products)
	//获取error
	fmt.Println(res.Error)
	for _, p := range products {
		fmt.Println(p.ID)
	}
}
